package com.qipay.admin.card.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.card.service.CardBindLogicService;
import com.qipay.admin.card.vo.CardBindVo;
import com.qipay.admin.tags.PageModel;
import com.qipay.baas.model.Cp;
import com.qipay.cp.CpService;
import com.qipay.security.UserDetailsServiceImpl;
import com.qipay.user.entity.UserLevel;
import com.qipay.utils.Result;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Creator: Yao
 * Date:    2017/10/19
 * For:
 * Other:
 */
@Controller
@RequestMapping("/card/bind")
public class CardBindController extends BaseController {

    @Autowired
    CardBindLogicService cardBindLogicService;
    @Autowired
    CpService cpService;

    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public ModelAndView list(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @RequestParam(value = "p", required = false, defaultValue = "1") int p,
            @RequestParam(value = "pz", required = false, defaultValue = "10") int pz) {

        // check
        Assert.notNull(loginUser);
        List<CardBindVo> lst = null;
        PageModel pageModel = new PageModel();
        long total = 0;
        p = p < 1 ? 1 : p;
        pz = pz <= 0 ? 10 : pz;
        int userId = loginUser.getId();

        // init
        ModelAndView view = createModelAndView("cardBind-lst");

        // query
        total = cardBindLogicService.count(userId);
        if (0 != total) {
            lst = cardBindLogicService.page(loginUser.getId(), p, pz);
        }

        // fill
        view.addObject("total", total);
        view.addObject("lst", lst);
        pageModel.setPage(p);
        pageModel.setPageSize(pz);
        pageModel.setTotal(total);
        view.addObject("page", pageModel);

        return view;
    }

    @RequestMapping(value = "/{bindId:\\d+}", method = RequestMethod.GET)
    public ModelAndView get(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            @PathVariable(name = "bindId", required = true) Long bindId) {

        // check
        Assert.notNull(loginUser);

        // init
        CardBindVo cardBind = null;
        ModelAndView view = createModelAndView("cardBind-detail");

        //
        if (null != bindId && 0 != bindId) {
            cardBind = cardBindLogicService.get(bindId);
        } else {
            cardBind = new CardBindVo();
            cardBind.setId(0l);
            cardBind.setUserId(Long.valueOf(loginUser.getId()));
            cardBind.setBank("");
            cardBind.setSubBank("");
            cardBind.setCard("");
            cardBind.setName(loginUser.getUsername());
            if (loginUser.getLevel() != UserLevel.ADMIN) {
                Cp cp = cpService.getOneById(Long.parseLong(loginUser.getUsername()));
                cardBind.setMobile(null != cp ? cp.getBindMobile() : "");
            } else {
                cardBind.setMobile("");
            }
        }
        view.addObject("detail", cardBind);

        return view;
    }

    @RequestMapping(value = {"", "/"}, method = RequestMethod.POST)
    public ModelAndView save(
            @AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
            CardBindVo cardBind) {

        // save
        cardBindLogicService.save(loginUser, cardBind);

        return list(loginUser, 1, 10);
    }

    @RequestMapping(value = "/{bindId:\\d+}", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@PathVariable(name = "bindId", required = true) Long bindId) {

        // init
        Result result = new Result<>();

        // logic
        try {
            cardBindLogicService.delete(bindId);
            result.setCode(200);
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg(e.getMessage());
        }

        return result;
    }
}
